package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.MergeVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseItemDoneVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().lambda().eq(PurchaseEntity::getStatus,WareConstant.PurchaseStatusEnum.CREATED.getCode()).or().eq(PurchaseEntity::getStatus,WareConstant.PurchaseStatusEnum.ASSIGNED.getCode())
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        List<Long> items = mergeVo.getItems();
        items.forEach(System.out::println);
        LambdaUpdateWrapper<PurchaseDetailEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PurchaseDetailEntity::getPurchaseId,purchaseId)
                .set(PurchaseDetailEntity::getStatus,WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode())
                .in(PurchaseDetailEntity::getId,items);
        purchaseDetailService.update(updateWrapper);
    }
    @Transactional
    @Override
    public void received(List<Long> ids) {
        List<Long> filterIds = ids.stream().filter(id -> {
                    PurchaseEntity byId = this.getById(id);
                    return byId.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() || byId.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode();
                }
        ).collect(Collectors.toList());
        LambdaUpdateWrapper<PurchaseEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PurchaseEntity::getStatus,WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
        wrapper.in(PurchaseEntity::getId,ids);
        this.update(wrapper);
        LambdaUpdateWrapper<PurchaseDetailEntity> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.set(PurchaseDetailEntity::getStatus,WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
        wrapper1.in(PurchaseDetailEntity::getPurchaseId,filterIds);
        purchaseDetailService.update(wrapper1);
    }
    @Transactional
    @Override
    public void done(PurchaseDoneVo purchaseDoneVo) {
        Long doneVoId = purchaseDoneVo.getId();
        AtomicReference<Boolean> flag= new AtomicReference<>(true);
        List<PurchaseItemDoneVo> items = purchaseDoneVo.getItem();
        for (PurchaseItemDoneVo vo:items){
            if (vo.getStatus()==WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag.set(false);
            }
        }
        List<PurchaseDetailEntity> collect = items.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                    if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                        flag.set(false);
                        purchaseDetailEntity.setStatus( WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
                    }else {
                        purchaseDetailEntity.setStatus( WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                        PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());
                        wareSkuService.andStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());
                    }
                    purchaseDetailEntity.setId(item.getItemId());
                    return purchaseDetailEntity;
                }
        ).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        LambdaUpdateWrapper<PurchaseEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(flag.get(),PurchaseEntity::getStatus,WareConstant.PurchaseStatusEnum.FINISH.getCode());
            wrapper.set(!flag.get(),PurchaseEntity::getStatus,WareConstant.PurchaseStatusEnum.HASERROR.getCode());
            wrapper.eq(PurchaseEntity::getId,doneVoId);
            this.update(wrapper);

    }
}